/**
 * 
 */
package cn.edu.bjtu.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.bjtu.dao.OperationDbDao;
import cn.edu.bjtu.pojo.AuthorEntity;
import cn.edu.bjtu.pojo.CityStateInfo;
import cn.edu.bjtu.pojo.TeamInfo;


/**
 * @ClassName: MysqlOperationDaoImpl
 * @Description: TODO MySql direct operation of the database, other operations
 *               can be added in the corresponding interface(@see
 *               alpha.huizhi.dao.OperationDbDao)
 * @author: Richard
 * @ChineseName Li Yao
 * @date 2017/10/24 PM 11:18:51 
 * 
 */
@Repository("mysql")
public class MysqlOperationDaoImpl extends MysqlDbDao implements OperationDbDao {
	protected static Logger logger = LoggerFactory.getLogger(MysqlOperationDaoImpl.class);
	String result;
	@Override
	public String login(AuthorEntity user) {
		Session s = currentSession();
		// 根据节点id //
		String findSql = " from  AuthorEntity as m where m.UserNo=:UserNo and m.Password=:Password ";
		// String findSql = "from NodeInformation as n inner join fetch
		// n.sensors s where n.modelId=:modelId order by n.timestamp desc";
		Query query = s.createQuery(findSql).setString("UserNo",user.getUserNo()).setString("Password", user.getPassword());
		 List<AuthorEntity> users = query.list();
		 if(users!=null && users.size() > 0)
		   return "success";
		 else
			 return "fail";
	}
	@Override
	public String getUserRole(AuthorEntity user) {
		
		Session s = currentSession();
		// 根据节点id //
		String findSql = " from  AuthorEntity as m where m.UserNo=:UserNo and m.Password=:Password ";
		// String findSql = "from NodeInformation as n inner join fetch
		// n.sensors s where n.modelId=:modelId order by n.timestamp desc";
		Query query = s.createQuery(findSql).setString("UserNo",user.getUserNo()).setString("Password", user.getPassword());
		 List<AuthorEntity> users = query.list();
		 if(users!=null && users.size() > 0)
		   return users.get(0).getUserRole();
		 else
			 return "fail";
		
	}
	@Override
	public List<TeamInfo> getTeamUser() {
		Session s = currentSession();
		String sql = "from TeamInfo";
		Query	query = s.createQuery(sql);
		List<TeamInfo> teams = query.list();
		return teams;
	}
	@Override
	public List<CityStateInfo> getCityStateInfo() {
		// TODO Auto-generated method stub
		Session s = currentSession();
		String sql = "from CityStateInfo";
		Query	query = s.createQuery(sql);
		List<CityStateInfo> teams = query.list();
		return teams;
	}
	@Override
	@Transactional(propagation = Propagation.MANDATORY) 
	public boolean deleteCityStateInfo(String id) {
		boolean result = false;
		try{
			Session s = currentSession();
			String sql = "delete CityStateInfo where cityID=:cityID";
			int row = s.createQuery(sql).setString("cityID", id).executeUpdate();
			if(row > 0){
				result = true;
			}else{
				result = false;
			}
		}catch(Exception e){
			result = false;
	}
	return result;
	}
	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public boolean addCityStateInfo(CityStateInfo cityStateInfo) {
		boolean result = false;
		try{
			this.getHibernateTemplate().save(cityStateInfo);
			result = true;
		}catch(Exception e){
			result = false;
		}
		return result;
	}
	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public boolean updateStateInfo(CityStateInfo cityStateInfo) {
		boolean result = false;
		try{
			this.getHibernateTemplate().saveOrUpdate(cityStateInfo);
			result = true;
		}catch(Exception e){
			result = false;
		}
		return result;
	}
	

	

}
